import md5 from 'js-md5'

const getConfig = (field, id, ret) => {
	let data
	if (getApp().globalData.config[field]) {
		if (Array.isArray(getApp().globalData.config[field])) {
			getApp().globalData.config[field].forEach(v => {
				if (v.id == id) {
					data = v
				}
			})
		} else {
			data = getApp().globalData.config[field]
		}
	}
	return data ? (ret ? data[ret] : data) : ''
}

const getCache = (name) => {
	let sign = md5(name)
	let time = Date.parse(new Date()) / 1000
	let json = uni.getStorageSync(sign)
	if (json) {
		let cache = JSON.parse(json || '{}')
		if (cache.time > 0 && cache.time > time) {
			return cache.data
		} else {
			uni.removeStorageSync(sign)
		}
	}
}

const setCache = (name, data, time = 3600) => {
	let sign = md5(name)
	let cachetime = Date.parse(new Date()) / 1000 + time
	let cache = {
		data: data,
		time: cachetime
	}
	uni.setStorageSync(sign, JSON.stringify(cache))
	return true
}

const imageError = (item, field = 'pic') => {
	item[field] = '/static/nopic.jpg'
}

const goTop = () => {
	uni.pageScrollTo({
		scrollTop: 0,
		duration: 300
	})
}

const goTo = (url) => {
	if (url.indexOf('/pages/tabBar1') > -1 || url.indexOf('/pages/tabBar2') > -1 || url.indexOf('/pages/tabBar3') > -1 || url.indexOf('/pages/tabBar4') > -1 || url.indexOf('/pages/tabBar5') > -1){
		let arr = url.split('?')
		if (arr.length > 1) {
			let options = {}
			let param = arr[1].split('&')
			param.forEach(item => {
				let temp = item.split('=')
				options[temp[0]] = temp[1]
			})
			uni.setStorageSync('options', options)
		}
		uni.switchTab({
			url: arr[0]
		})
	} else if (url.substr(0, 1) != '/') {
		let arr = url.split('/')
		let appid = arr[0]
		let path = url.replace(appid, '')
		let options = {}
		arr = url.split('?')
		if (arr.length > 1) {
			let param = arr[1].split('&')
			param.forEach(item => {
				let temp = item.split('=')
				options[temp[0]] = temp[1]
			})
		}
		uni.navigateToMiniProgram({
			appId: appid,
			path: 'pages/index/index?id=123',
			extraData: options,
			success(res) {}
		})
	} else {
		uni.navigateTo({
			url: url
		})
	}
}

const http = (method, path, param, headers = {}) => {
	uni.showLoading()
	return new Promise((resolve, reject) => {
		uni.request({
			url: path,
			method: method,
			data: param,
			header: headers,
			success: res => {
				resolve(res.data)
			},
			complete() {
				uni.hideLoading()
			}
		})
	})
}

const copyData = (data) => {
	uni.setClipboardData({
		data: data,
		success: function() {
			uni.showToast({
				title: '复制成功!',
				icon: 'success',
				duration: 3000
			})
		}
	})
}

const inArray = (arr = [], key) => {
	if (arr.length > 0) {
		return arr.toString().indexOf(key) > -1 ? true : false
	} else {
		return false
	}
}

const getArrayIndex = (arr = [], key, field) => {
	let index = -1
	arr.forEach((v, k) => {
		if (field) {
			if (v[field] === key) {
				index = k
			}
		} else {
			if (v === key) {
				index = k
			}
		}
	})
	return index
}

const getArrayName = (arr = [], id) => {
	let data
	arr.forEach(v => {
		if (Array.isArray(id)) {
			data = data || []
			if (inArray(id, v.id)) {
				data.push(v.name)
			}
		} else if (v.id == id) {
			data = v.name
		}
	})
	return data
}
const putCustomFormData = (form, data) => {
	let newForm = JSON.parse(JSON.stringify(form))
	if (!data) {
		return newForm
	}
	newForm.forEach(v => {
		if (data[v.field]) {
			v.value = data[v.field]
		}
	})
	return newForm
}
const getCustomFormData = (form) => {
	let json = {}
	form.forEach(v => {
		json[v.field] = v.value
	})
	return json
}

export {
	getCache,
	setCache,
	goTop,
	goTo,
	copyData,
	imageError,
	http,
	inArray,
	getArrayName,
	getArrayIndex,
	putCustomFormData,
	getCustomFormData,
	getConfig,
}
